<?php
namespace Common\Model;
use Common\Model\VRLiveModel;
class LiveResourceVideoModel extends VRLiveModel
{
	
	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
	);

	public function get_list($live_id){
		return $this->where("live_id=$live_id and status <> 0")->field('thumbnail_url,video_url,video_id,video_name,file_name,ifnull(input_file_id,file_id) as file_id,input_file_id,category')->select();
	}
	
	/**
	 * 上传视频
	 */
	public function uploadVideo($video_id = null, $is_vr = '0'){
		if(!empty($_FILES) && !empty($_FILES['file']['name'])){
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     899614161 ;// 设置附件上传大小
			$upload->exts      =     array('avi', 'mp4', 'rmvb', 'flv', 'asf', 'rm', '3gp', 'wmv', 'mpeg', 'mpg');// 设置附件上传类型
			$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
			
			// 上传文件
			$info = $upload->upload();
			
			if(!$info) {// 上传错误提示错误信息
				var_dump($upload->getError());exit;
				return false;
			}else{
				$files_name = "file";
				$videoName = $info[$files_name]['name'];
				$size = $info[$files_name]['size'];
				$ext = $info[$files_name]['ext'];
				$sha1 = $info[$files_name]['sha1'];
				$savename = C('UPLOADPATH').$info[$files_name]['savepath'].$info[$files_name]['savename'];
				$config = array('SecretId'       => C("API_SecretId"),
						'SecretKey'      => C("API_SecretKey"),
						'RequestMethod'  => 'POST',
						'DefaultRegion'  => C("API_DefaultRegion"));
			
				vendor("QcloudApi.QcloudApi");
				$service = \QcloudApi::load(\QcloudApi::MODULE_VOD, $config);
				//要输入的参数
				$package = array(
						'fileName' => $savename,                                              //文件的绝对路径，包含文件名
						'dataSize' => $size,                                          //分片大小，建议使用默认值5MB
						'isTranscode' => 1,                                                  //是否转码
						'isScreenshot' => 1,                                                 //是否截图
						'isWatermark' => 0,                                                  //是否添加水印
						'notifyUrl' =>$_SERVER['SERVER_NAME'].'/'.C("notify_url")."&type=3"."&is_vr=".$is_vr."&key=".sha1(md5(".$video_id.")+"_xhw")
				);
				$a = $service->MultipartUploadVodFile($package);
				\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
				if($a == false){
					return false;
				}else{
			
					$fileId = $a['fileId'];
					//删除本地文件
					unlink($savename);
					return $fileId;
				}
			}
		}
		return false;
	}
	
	public function get_video($fileId = null, $category){
		if($fileId){
			$config = array('SecretId'       => C("API_SecretId"),
					'SecretKey'      => C("API_SecretKey"),
					'RequestMethod'  => 'GET',
					'DefaultRegion'  => C("API_DefaultRegion"));
			vendor("QcloudApi.QcloudApi");
			$service = \QcloudApi::load(\QcloudApi::MODULE_VIDEO, $config);
			$package = array(
					'fileIds.1'=>$fileId,
					'status'=>2
			);
			$result = $service->DescribeVodInfo($package);
			\Think\Log::record('腾讯云返回：'.json_encode($result), 'INFO');
			if(0 == $result['code']){
				$fileSet = $result['fileSet'];
				if($fileSet && !empty($fileSet)){
					$file = $fileSet[0];
					$data = array();
					
					$data['thumbnail_url'] = $file['imageUrl'];
					$data['file_name'] = $file['fileName'];
					$data['duration'] = $this->changeTimeType($value['duration']);
					$data['size'] = $file['size'];
					//要输入的参数
					//14651978969340560036
					$package = array(
							'fileId'=>$fileId
					);
					$result = $service->DescribeVodPlayUrls($package);
					\Think\Log::record('腾讯云返回：'.json_encode($result), 'INFO');
					if(0 == $result['code']){
						$playSet = $result['playSet'];
						if($playSet && !empty($playSet)){
							foreach($playSet as $key1=>$value1){
// 								if($category == '1'){
// 									if($value1['definition'] == '0'){
// 										$data['video_url'] = $value1['url'];
// 										$data['height'] = $value1['vheight'];
// 										$data['width'] = $value1['vwidth'];
// 									}
// 								}else{
									if($value1['definition'] == '30'){
										$data['video_url'] = $value1['url'];
										$data['height'] = $value1['vheight'];
										$data['width'] = $value1['vwidth'];
									}
// 								}
								
								 
								if($value1['definition'] == '20'){
									$data['video_url1'] = $value1['url'];
									$data['height1'] = $value1['vheight'];
									$data['width1'] = $value1['vwidth'];
									$data['size1'] = $data['size'];
								}
								 
								if($value1['definition'] == '10'){
									$data['video_url2'] = $value1['url'];
									$data['height2'] = $value1['vheight'];
									$data['width2'] = $value1['vwidth'];
									$data['size2'] = $data['size'];
								}
								 
							}
							return $data;
						}else{
							return -2;
						}
					}else{
						return -1;
					}
				}else{
					return -2;
				}
			}else{
				return -1;
			}
		}
		return 0;
	}
	
	function changeTimeType($seconds) {
		if ($seconds > 3600) {
			$hours = intval($seconds / 3600);
			$minutes = $seconds % 3600;
			$time = $hours . ":" . gmstrftime('%M:%S', $minutes);
		} else {
			$time = gmstrftime('%H:%M:%S', $seconds);
		}
		return $time;
	}
}

